<?php
/**
 * lsys orm
 * @author     Lonely <shan.liu@msn.com>
 * @copyright  (c) 2017 Lonely <shan.liu@msn.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0
 */
namespace LSYS\ORMCache\Cache;
use LSYS\ORM\Cache;
use LSYS\Config;
use LSYS\Config\SubSet;
use LSYS\ShareMemcache;
class Memcache extends Cache implements ShareMemcache{
	use \LSYS\ShareMemcache\MemcacheShare;
	/**
	 * @var \Memcache
	 */
	protected $_memcache;
	/**
	 * @param Config $config
	 */
	public function __construct(Config $config=null){
		parent::__construct($config);
		$_config=$config->exist("config")?new SubSet($config, "config"):null;
		$this->_memcache=self::get_share_memcache($_config);
	}
	protected function _set_where($pk,$data,$timeout){
		if ($timeout>=2592000)$timeout=time()+$timeout;
		else if ($timeout<=0)$timeout=0;
		return $this->_memcache->set('_L_O_W_'.$pk,$data,MEMCACHE_COMPRESSED,$timeout);
	}
	protected function _get_where($pk){
		return  $this->_memcache->get('_L_O_W_'.$pk);
	}
	protected function _set_orm($pk,array $data,$timeout){
		$pk='_L_O_P_'.$pk;
		RETURN $this->_memcache->set($pk,serialize($data),MEMCACHE_COMPRESSED,$timeout);
	}
	protected function _get_orm($pk){
		$pk='_L_O_P_'.$pk;
		$data=$this->_memcache->get($pk);
		if ($data==null) return false;
		return unserialize($data);
	}
	protected function _exist_orm($pk){
		$pk='_L_O_P_'.$pk;
		return $this->_memcache->get($pk)!==null;
	}
	protected function _del_orm($pk){
		$pk='_L_O_P_'.$pk;
		return $this->_memcache->delete($pk);
	}
}